#!/bin/sh
# - runtime
# - test
# - nr_task

## UnixBench is the original BYTE UNIX benchmark suite. The purpose
## of UnixBench is to provide a basic indicator of the performance
## of a Unix-like system; hence, multiple tests are used to test
## various aspects of the system's performance.

. $LKP_SRC/lib/reproduce-log.sh
. $LKP_SRC/lib/upload.sh

yum install numactl -y
mount_test(){
        os=$(pvs | sed -n '2p;'| awk '{print $1}')
        if [[ $os == *sda ]];then
               dev=sda
        elif [[ $os==*sdb ]];then
               dev=sdb
        elif [[ $os==*sdc ]];then
               dev=sdc
        fi
        capacit=$(lsblk | grep $dev |awk '{print $4}')
        pan=$(lsblk | grep $capacit | awk '{print $1}' | grep -v $dev | grep -o "^[sh]d[a-z]")
        disk=$(fdisk -l | grep -o "^Disk /dev/[sh]d[a-z]"| grep $pan |awk '{print $2}')
        echo $disk

        echo '------------ ready mount $disk ------------'
        parted -s ${disk} mklabel gpt
        parted ${disk} mkpart ext4 0% 100%
        mkfs -t ext4 ${disk}1 &>/dev/null
        mkdir /test &>/dev/null
        mount ${disk}1 /test
        echo '------------ mount success -----------'
}

#lsblk | grep /test
#if [[ $? -eq 1 ]];then
#        mount_test
#else
#	disk=$(df -h | grep /test | awk '{print $1}')
#	umount /test
#	rm -rf /test
#	mkdir /test
#	mkfs.ext4 -F $disk
#	mount $disk /test
#fi

unixbench_path=/test/benchmarks/unixbench
#[ -n "$mount_to" ] && {
#	cp -ra $BENCHMARK_ROOT /$mount_to/
#	unixbench_path=/$mount_to/benchmarks/unixbench
#}

test_partial()
{
	export LANG=C

	[ -n "$runtime" ] || runtime=300

	# Each iteration in UnixBench is about 10 seconds
	iteration=$(( runtime / 10 ))

	[ $test = "shell8" ] && iteration=1

	[ -n "$nr_task" ] && other_params="-c $nr_task"

	cd UnixBench
	log_cmd ./Run $test $other_params -i $iteration || exit
}

test_all()
{

	cd UnixBench
	echo 3 > /proc/sys/vm/drop_caches
	log_cmd use_benchmark_env numactl --interleave=all ./Run -c $nr_task
}

cd $unixbench_path || die "no $unixbench_path"

if [ -n "$mount_to" ]; then
	test_all
else
	test_partial
fi

upload_files -t results $unixbench_path/UnixBench/results/*
